<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Request;
class Chat extends Controller
{
	/**
	 * 获取token
	 */
	public function getToken()
	{
		// var_dump($this->get_rong_key_secret());die();
       $uid = Session::get('adminUserId');
       $uid = '9';
       // $uid = ayyay('9','10','11');
        // 获取token
        $token = $this->get_rongcloud_token($uid);
        //判断token是否存在
        if($token && $token!=false){
	        return $token;
        }else{
        	//token不存在
        	return '';
        }     
	}
	/**
	 * 根据配置项获取对应的key和secret
	 * @return array key和secret
	 */
	private function get_rong_key_secret()
	{
	    $key=Config('APP_KEY');
	    $secret=Config('APP_SECRET');
	    $data=array(
	        'key'=>$key,
	        'secret'=>$secret
	        );
	    return $data;

	}
	/**
	 * 引入外部类并实例化类
	 */
	private function get_rong_cloud()
	{
		vendor('rongyun.rongcloud');
		// 获取key和secret
	    $key_secret=$this->get_rong_key_secret();
	    // 实例化融云
	    $rong_cloud=new \RongCloud($key_secret['key'],$key_secret['secret']);

	    return $rong_cloud;
	}
	/**
	 * 获取融云token
	 * @param  integer $uid 用户id
	 * @return integer      token
	 */
	private function get_rongcloud_token($uid)
	{
	    // 从数据库中获取token
	    $oauthUser=Db('oauth_user')->where('uid',$uid)->where('type',2)->find();
	    // var_dump($oauthUser);die();
	    // 如果有token就返回
	    if(!empty($oauthUser)) {
	        return $oauthUser['access_token'];
	    }
	    // 获取用户昵称和头像
	    $user=Db('adminuser')->where('id',$uid)->field('username')->find();
	    // 用户不存在
	    if (empty($user)) {
	        return false;
	    }
	    // 获取头像url格式
	    $pic = 'http://api.zgyejy.net/uploads/video/images/20161106/90fdecb7af2cd23d004b45f1f6f2771e.png';
	    // $pic = $this->getIconUrl($uid);
	    // var_dump($pic);die();

	    // 实例化融云
	    $rong_cloud=$this->get_rong_cloud();
	    // 获取token
	    $token_json=$rong_cloud->user()->getToken($uid,$user['username'],$pic);
	    $token_array=json_decode($token_json,true);
	    // 获取token失败
	    if ($token_array['code']!=200) {
	        return false;
	    }
	    $token=$token_array['token'];
	    $data=array(
	        'uid'=>$uid,
	        'nickname'=>$user['username'],
	        'pic'=>$pic,
	        'access_token'=>$token,
	        'create_time' => time(),
	        'type' => 2
	        );
	    // 插入数据库
	    $result=Db('oauth_user')->insert($data);
	    // var_dump($result);die();
	    if ($result > 0) {
	        return $token;
	    }else{
	        return false;
	    }
	}

	public function benneg()
	{
		$uid = Session::get('adminUserId');

		$users = $this->queryUser();
		
		foreach ($users as $k => $v){
			if($uid != $v['id']){
				$res = $this->add_chatroom_gag_user($v['id'],'10001','60');
				echo 1;
			}
		}
	}

	/**
	 * 查询聊天室成员
	 */
	private function queryUser()
	{
		$chatroomId = '10001';
		// 实例化融云
	    $rong_cloud=$this->get_rong_cloud();
	    //创建聊天室
	    $result_json=$rong_cloud->chatroom()->queryUser($chatroomId, '500', '2');
	    $result_array=json_decode($result_json,true);
	    if ($result_array['code']==200) {
	    	// $result = array(
	    	// 	'total' => $result_array['total'],
	    		$users = $result_array['users'];
	    		// );
	        return $users;
	    }else{
	        return '';
	    }
	}
	/**
	 * 聊天室禁言用户
	 */
	private function add_chatroom_gag_user($userId,$chatroomId,$minute)
	{
		// 实例化融云
	    $rong_cloud=$this->get_rong_cloud();
	    //创建聊天室
	    $result_json=$rong_cloud->chatroom()->addGagUser($userId, $chatroomId, $minute);
	    $result_array=json_decode($result_json,true);
	    if ($result_array['code']==200) {
	        return true;
	    }else{
	        return false;
	    }
	}

	public function message()
	{
		$fromUserId = Session::get('adminUserId');
		$toChatroomId = '10001';
		$objectName = 'RC:TxtMsg';
		$content = input('get.content');
		$message = array(
			'content' => $content
			);
		$message = json_encode($message);
		$rong_cloud=$this->get_rong_cloud();
	    //创建聊天室
	    $result_json=$rong_cloud->message()->publishChatroom($fromUserId,$toChatroomId,$objectName,$message);
	    $result_array=json_decode($result_json,true);
	    if ($result_array['code']==200) {
	    	echo 1;
	    }else{
	        echo 0;
	    }
		
	}

	public function jiejin()
	{
		$userId = input('get.uid');
		$chatroomId = '10001';
		// 实例化融云
	    $rong_cloud=$this->get_rong_cloud();
	    //创建聊天室
	    $result_json=$rong_cloud->chatroom()->rollbackGagUser($userId,$chatroomId);;
	    $result_array=json_decode($result_json,true);
	    if ($result_array['code']==200) {
	        echo 1;
	    }else{
	        echo 0;
	    }    		    
	}

	public function chat()
	{
		return $this->fetch();
	}


}